package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.dto.Equipment_Scheme_DTO;
import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.entity.EquipmentList;
import com.six666.medicaldevicetestingsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class Equipment_Controller {
    @Autowired
    private Equipment_Service equipment_service;
    @Autowired
    private Equipment_Scheme_Service equipment_scheme_service;
    @Autowired
    private AccountOrder_Service account_order_service;
    @Autowired
    ProjectFile_Service project_file_service;
    @Autowired
    File_Service file_service;
    @PostMapping("/equipments_by_project")
    public BaseResponse<List<EquipmentList>> equipments_by_project(@RequestParam Map<String,String> params) {
        int project_id;
        if (params.get("project_id")!=null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        else{
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        List<EquipmentList> res=equipment_service.get_equipment_by_project(project_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/schemes_by_equipment")
    public BaseResponse<List<Equipment_Scheme_DTO>>schemes_by_equipment(@RequestParam Map<String,String> params){
        int equipment_id;
        if (params.get("equipment_id")!=null) {
            try {
                equipment_id = Integer.parseInt(params.get("equipment_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "equipment_id非整形");
            }
        }
        else{
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        List<Equipment_Scheme_DTO>res=equipment_service.get_scheme_by_equipment(equipment_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/add_equip_order")
    public BaseResponse<Boolean> add_equip_order(@RequestParam Map<String,String> params) {
        int account_id,goods_id,goods_amount;
        try {
            account_id = Integer.parseInt(params.get("account_id"));
            goods_id = Integer.parseInt(params.get("goods_id"));
            goods_amount = Integer.parseInt(params.get("goods_amount"));
        }
        catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(102, null, "参数错误", "account_id、goods_id、goods_amount为空或非整形");
        }
        boolean res=account_order_service.add_order(account_id,"Equipment",goods_id,goods_amount);
        return new BaseResponse<>(res);
    }
    @PostMapping("add_equip")
    public BaseResponse<Boolean> add_equip(@RequestParam Map<String,String> params) {
        int project_id;
        String equipment_name;
        equipment_name=params.get("equipment_name");
        if (equipment_name==null||params.get("project_id")==null) {
            return new BaseResponse<>(101, null, "参数错误", "必填值为空");
        }
        try {
            project_id = Integer.parseInt(params.get("project_id"));
        }
        catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
        }
        return new BaseResponse<>(equipment_service.add_equip(project_id,equipment_name));
    }
    @PostMapping("/add_equip_pic")
    public BaseResponse<Boolean> add_equip_pic(@RequestParam("file") MultipartFile file,
                                                   @RequestParam("scheme_id") int scheme_id) {
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = "equip" + scheme_id + "_#" + file.getOriginalFilename();
        boolean res=file_service.save_file(file,"pic",fileName);
        if (!res){
            return new BaseResponse<>(false);
        }
        int equip_id=equipment_scheme_service.get_equip_id_by_scheme_id(scheme_id);
        int project_id=equipment_service.get_project_id_by_equipment_id(equip_id);
        //project_file_service.delete_file_by_project_id_and_type(project_id,"Picture");
        int pic_id = project_file_service.add_project_file_get_id(project_id,"Picture","pic/"+fileName,fileName);
        res=equipment_scheme_service.add_or_update_pic(scheme_id,pic_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/add_equip_pdf")
    public BaseResponse<Boolean> add_equip_pdf(@RequestParam("file") MultipartFile file,
                                               @RequestParam("scheme_id") int scheme_id) {
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = "equip" + scheme_id + "_#" + file.getOriginalFilename();
        boolean res=file_service.save_file(file,"pdf",fileName);
        if (!res){
            return new BaseResponse<>(false);
        }
        int equip_id=equipment_scheme_service.get_equip_id_by_scheme_id(scheme_id);
        int project_id=equipment_service.get_project_id_by_equipment_id(equip_id);
        //project_file_service.delete_file_by_project_id_and_type(project_id,"EquipmentScheme");
        int pdf_id=project_file_service.add_project_file_get_id(project_id,"EquipmentScheme","pdf/"+fileName,fileName);
        res=equipment_scheme_service.add_or_update_pdf(scheme_id,pdf_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("add_equip_scheme")
    public BaseResponse<Boolean> add_equip_scheme(@RequestParam Map<String,String> params) {
        int equipment_id;
        try {
            equipment_id=Integer.parseInt(params.get("equipment_id"));
        }
        catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101,null,"参数错误","equipment_id为空或非整形");
        }
        String name= params.get("name");
        String source= params.get("source");
        if (name==null||source==null) {
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        return new BaseResponse<>(equipment_scheme_service.add_scheme(equipment_id,name,source));
    }
}
